package com.java.simple.study.juc.p7;

import com.java.simple.study.tool.Sleeper;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.SynchronousQueue;

/**
 * @author zhoujunlin
 * @date 2022年08月04日 21:57
 * @desc
 */
@Slf4j(topic = "c.TestSynchronousQueue")
public class TestSynchronousQueue {

    public static void main(String[] args) {
        SynchronousQueue<Integer> intQueue = new SynchronousQueue<>();

        new Thread(() -> {
            try {
                log.debug("put 1 into queue");
                // SynchronousQueue队列put必须要有其他线程取才能put成功
                // 这里会阻塞住 等到有线程来去
                intQueue.put(1);
                log.debug("put 1 success");

                log.debug("put 2 into queue");
                intQueue.put(2);
                log.debug("put 2 success");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "t1").start();
        //停止2秒 t1线程put等待
        Sleeper.sleep(2);

        new Thread(() -> {
            try {
                log.debug("taking");
                Integer result = intQueue.take();
                log.debug("taked result:{}", result);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "t2").start();
        Sleeper.sleep(2);

        new Thread(() -> {
            try {
                log.debug("taking");
                Integer result = intQueue.take();
                log.debug("taked result:{}", result);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "t3").start();


    }


}
